const path = require('path');
const webpack = require('webpack');
// 提取到单独的css文件
const ExtractTextPlugin = require("extract-text-webpack-plugin");
// 简化了HTML文件的创建
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 配置ExtractTextPlugin
const ExtractTextConfig = new ExtractTextPlugin({
  filename: 'css/[name].[contenthash].css',
  disable: process.env.NODE_ENV === "development"
});

const cwd = process.cwd();
const getHtml = require('./util/getHtml.js');
const getEntry = require('./util/getEntry.js');
// getEntry["manifest"] = ['jquery'];
const getChunk = require('./util/getChunk.js');
const entryList = getEntry(path.resolve(cwd, 'src/js/page/**/*.js'));
const htmlPluginList = getHtml(path.resolve(cwd, 'src/view/**/*.html'));
const chunksList = Object.keys(entryList)
const chunkPlugin = new webpack.optimize.CommonsChunkPlugin({
    name: 'vendors',
    chunks: chunksList,
    minChunks: chunksList.length
})

console.log(entryList)
console.log(htmlPluginList)
function resolve(dir){
  return path.join(cwd, dir)
}

const plugins = [
    // 自动加载模块
    // new webpack.ProvidePlugin({
    //     $: 'zepto'
    // }),
    ExtractTextConfig,
    chunkPlugin
    // new webpack.optimize.CommonsChunkPlugin({
    //   name: "manifest",
    //   minChunks: Infinity
    // })
    // 提取公共代码
    // new webpack.optimize.CommonsChunkPlugin({
    //     name: 'vendors',
    //     chunks: ['index','about'],
    //     minChunks: 2
    // }),
    // new HtmlWebpackPlugin({
    //     filename: 'view/index.html',
    //     template: './src/view/index.html',
    //     inject: 'body',
    //     chunks: ['vendors', 'index'],
    //     hash: false,
    //     minify: process.env.NODE_ENV !== 'production' ? false : {
    //       removeAttributeQuotes: true,
    //       removeComments: true,
    //       collapseWhitespace: true,
    //       html5: true,
    //       minifyCSS: true
    //     }
    // })
].concat(htmlPluginList);



module.exports = {
  entry: entryList,
  output: {
    path: path.resolve(cwd, './dist'),
    // publicPath: '/dist/',
    filename: 'js/[name].js',
    chunkFilename: 'js/[id].chunk.js'
  },
  resolve: {
    extensions: ['.js','.vue','.json'],
    alias: {
      '@': resolve('src'),
      'css': resolve('src/css')
    }
  },
  module: {
    rules: [
      {
        test: /\.(html)$/,
        use: {
          loader: 'html-loader',
          options: {
            attrs: [':data-src']
          }
        }
      },
      // 处理css文件
      {
        test: /\.css$/,
        use: ExtractTextConfig.extract({
            use: [{
                loader: "css-loader"
            },{
                loader: "postcss-loader",
                options: {
                  ident: 'postcss',
                  plugins: [
                    // require('autoprefixer')(),
                    require('postcss-cssnext')()
                  ]
                }
            }],
            // 在开发环境使用 style-loader
            fallback: "style-loader"
        })
      },
      // 处理scss文件
      {
        test: /\.scss$/,
        use: ExtractTextConfig.extract({
            use: [{
                loader: "css-loader"
            },{
                loader: "postcss-loader",
                options: {
                  ident: 'postcss',
                  plugins: [
                    // require('autoprefixer')(),
                    require('postcss-cssnext')()
                  ]
                }
            },{
                loader: "sass-loader"
            }],
            // 在开发环境使用 style-loader
            fallback: "style-loader",
        })
      },
      // 处理js文件
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
            loader: 'babel-loader',
            options: {
              presets: ['babel-preset-env']
            }
        }
      },
      //图片加载器，雷同file-loader，更适合图片，可以将较小的图片转成base64，减少http请求
      //如下配置，将小于8192byte的图片转成base64码
      {
        test: /\.(png|jpg|gif)$/,
        use: [{
            loader: 'url-loader',
            options: {
              limit: 8192,
              name: './img/[hash].[ext]'
            }
        }]
      },
      {
        test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        use: [{
            loader: 'file-loader',
            options: {
              name: './fonts/[name].[ext]'
            }
        }]
      }
      // exports-loader模块化zepto
      // https://sebastianblade.com/how-to-import-unmodular-library-like-zepto/
      // {
      //   test: require.resolve('zepto'),
      //   use: ['exports-loader?window.Zepto','script-loader']
      // }
    ]
  },
  plugins: plugins
};